//
//  CardTypeTableViewController.m
//  1243
//
//  Created by 杨森 on 16/5/11.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "CardTypeTableViewController.h"
#import "CardTypeTableViewCell.h"
#import "NetWorkTools.h"

@interface CardTypeTableViewController ()
@property(nonatomic,strong)NSMutableArray *boolArray;
@property(nonatomic,strong)NSArray *dataArr;
@property (nonatomic, strong) NSString *str;
@end

@implementation CardTypeTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择银行卡";
    [self data];
    
    }

- (NSMutableArray *)boolArray
{
    if (!_boolArray) {
        _boolArray = [NSMutableArray arrayWithObjects:@"1",@"0", @"0", @"0", @"0", @"0", @"0", @"0", @"0", @"0", nil];
    }
    return _boolArray;
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier= @"CardTypeTableViewCell";
    
    CardTypeTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell==nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"CardTypeTableViewCell" owner:nil options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    
    
//    
      if (self.dataArr.count) {
          NSString*string = self.dataArr[indexPath.row][@"Bank_Code"];
      //    [string substringFromIndex:15];
          self.str = string;
          if (string.length > 4) {
              self.str =  [string substringFromIndex:string.length - 4];
          }
//          NSLog(@"截取的值为：%@",str);
          cell.BankLabel.text = [NSString stringWithFormat:@"%@(%@)",self.dataArr[indexPath.row][@"Bank_Info"],self.str];
          

    }else{
        
        cell.BankLabel.text = @"";
        cell.selectImageView.hidden= YES;
    }
//
//    if ([self.boolArray[indexPath.row] isEqualToString:@"1"]) {
//        cell.selectImageView.image = [UIImage imageNamed:@"yixuan"];
//    } else {
//        cell.selectImageView.image = [UIImage imageNamed:@"weixuan"];
//    }
    
    return cell;
}


#pragma mark >>>>>>>> 代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString*string = self.dataArr[indexPath.row][@"Bank_Code"];
    self.str = string;
    if (string.length > 4) {
        self.str =  [string substringFromIndex:string.length - 4];
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeCard" object:nil userInfo:@{@"CardInfo":self.dataArr[indexPath.row][@"Bank_Info"], @"CardId":self.str}];
    [self.navigationController popViewControllerAnimated:YES];
    for (NSInteger i=0; i<self.boolArray.count; i++) {
        if (indexPath.row == i) {
            self.boolArray[i] = @"1";
        }else{
            self.boolArray[i] = @"0";
        }
    }
    [self.tableView reloadData];
}

- (void)data{
    NSDictionary *dict = [ReadLoginData readLoginData];
    
    //    NSLog(@"%@",dict);
    
    NSString *userNumber = [dict objectForKey:@"Number"];
    NSDictionary *params = @{
                             
                             @"UserNumber":userNumber,
                            
                             };
    
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"BANKINFOQUERY" completeBlock:^(id object) {
        NSLog(@"%@",object);
    
        NSString *string = [object objectForKey:@"BackData"];
        
        self.dataArr = [NetWorkTools arrayWithJsonString:string];
        [self.tableView reloadData];
    }];
}

@end
